﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using GeoWeb.DataContext;

namespace GeoWeb.Demo.DataContext
{
    /// <summary>
    /// 对DataContext类库的DEMO演示 数据库操作
    /// add by zy 
    /// 2013.01.14
    /// </summary>
    public class DemoDataDal : DbUtity
    {
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public int Add(DemoTable entity)
        {
            Db.BeginBatch();
            return Db.Save<DemoTable>(entity);
        }

        /// <summary>
        /// 查询 返回datatable
        /// </summary>
        /// <returns>泛型集合</returns>
        public List<DemoTable> Select()
        {
            return Db.From<DemoTable>().ToList() as List<DemoTable>;
        }

        /// <summary>
        /// 查询返回datatable
        /// </summary>
        /// <returns></returns>
        public DataTable Select2()
        {
            return Db.From<DemoTable>().ToTable() as DataTable;
        }

        /// <summary>
        /// 按条件查询
        /// </summary>
        /// <param name="Name">名称</param>
        /// <returns></returns>
        public List<DemoTable> SelectByName(string Name)
        {
            return Db.From<DemoTable>().Where(DemoTable._.Demo_Name == Name).ToList() as List<DemoTable>;
        }

        /// <summary>
        /// 按条件查询
        /// </summary>
        /// <param name="Name">名称</param>
        /// <returns></returns>
        public DemoTable SelectByID(int ID)
        {
            return Db.From<DemoTable>().Where(DemoTable._.Demo_ID == ID).ToSingle<DemoTable>() as DemoTable;
        }

        /// <summary>
        /// 根据ID进行删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(int id)
        {
            return Db.Delete<DemoTable>(DemoTable._.Demo_ID == id);
        }

        /// <summary>
        /// 根据名称进行修改
        /// </summary>
        /// <param name="oldName"></param>
        /// <param name="newName"></param>
        /// <returns></returns>
        public int UpdateByName(string oldName, string newName)
        {
            return Db.Update<DemoTable>(DemoTable._.Demo_Name, newName, DemoTable._.Demo_Name == oldName);
        }

        /// <summary>
        /// 修改一条记录
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int UpdateByEntity(DemoTable entity)
        {
            entity.Attach();
            return Db.InsertOrUpdate<DemoTable>(entity);
        }

        /// <summary>
        /// 执行SQL语句或者是存储过程   这里只写了写法  不做DEMO
        /// </summary>
        /// <returns></returns>
        public int ExceSQL()
        {
            string sql = string.Empty;
            SQLParameter[] parms = null;

            //执行存储过程或SQL的另一种写法
            Db.FromSql("exec procname @parm1").AddInputParameter("@parm1", DbType.Int32, "1");
            //------------------

            return Db.FromSql(sql, parms).Execute();//这个后面有很多方法   
        }

        /// <summary>
        /// 得到 刚插入的数据的ID 这里只写了写法   不做demo
        /// </summary>
        /// <returns></returns>
        public int GetInsertID(DemoTable entity)
        {
            int ID;
            InsertCreator ic = InsertCreator.NewCreator().From<DemoTable>().SetEntity<DemoTable>(entity);
            Db.Excute<int>(ic, out ID);
            return ID;
        }
    }
}